package com.feeyo.groundservice.usbserial.useage

import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.feeyo.groundservice.usbserial.BaseUsbSerialActivity
import com.feeyo.groundservice.usbserial.UsbCallback
import com.feeyo.groundservice.usbserial.UsbSerialImp

/**
 *作者：70893 on 2024-11-10
 *描述：只需关注回调即可
 */
class UsbSerialActivity : BaseUsbSerialActivity() {

    private var tv: TextView? = null
    private var btn: Button? = null
    private val stringBuilder = StringBuilder()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tv = TextView(this)
        btn = Button(this)
        //设置监听
        UsbSerialImp.setUsbCallback(object : UsbCallback {
            override fun onConnect() {

            }

            override fun onDisconnect() {

            }

            override fun onSuccess() {

            }

            override fun onReceive(data: ByteArray) {
                val msg = data.decodeToString()
                onLog(msg)

            }

            override fun onLog(msg: String) {
                stringBuilder.append(msg)
                stringBuilder.append("-")
                stringBuilder.append(UsbSerialImp.isConnected())
                stringBuilder.append("\n")
                tv?.text = stringBuilder.toString()
            }

            override fun onNotifyUser(msg: String) {
                Toast.makeText(this@UsbSerialActivity, msg, Toast.LENGTH_SHORT).show()
            }
        })

        setContentView(LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            addView(tv)
            addView(btn!!.apply {
                text = "发送数据"
                setOnClickListener {
                    UsbSerialImp.sendData("开始发送测试数据了${System.currentTimeMillis()}\n")
                }
            })
        })
    }

}